package day14;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.FileOutputStream;
import java.util.List;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class XMLDel {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        System.out.println("请输入要删除的序号:");

        int index=in.nextInt();

        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            //XMLRead.class.getClassLoader().getResourceAsStream("day14/demo.xml");
            doc = reader.read(XMLRead.class.getClassLoader().getResourceAsStream("day14/demo.xml"));
            //doc = reader.read(new FileInputStream("java高级\\src\\day14\\demo.xml"));
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        List<Element> list = doc.selectNodes("/books/book[@index=" + index + "]");//！！！！
        if(list.size()==0)
        {
            System.out.println("没找到！");
            return;
        }
        for(Element e:list){
            e.getParent().remove(e);///!!!!!!!!
        }
        try {
            OutputFormat format=OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            XMLWriter xm=new XMLWriter(new FileOutputStream("java高级\\src\\day14\\demo.xml"),format);
            xm.write(doc);
            xm.close();
        }  catch (Exception e) {
            e.printStackTrace();
        }
    }
}
